package com.koocloud.electroplatemanage.service.impl;

import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.AnalysisItemsMapper;
import com.koocloud.electroplatemanage.mapper.OperateMapper;
import com.koocloud.electroplatemanage.mapper.TankMapper;
import com.koocloud.electroplatemanage.pojo.*;
import com.koocloud.electroplatemanage.service.AnalysisItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program:
 * @description:
 * @author: wjx
 * @create: 2020/11/17 11:00
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AnalysisItemsServiceImpl implements AnalysisItemsService {


    @Resource
    private AnalysisItemsMapper analysisItemsMapper;

    @Resource
    private TankMapper tankMapper;

    @Resource
    private OperateMapper operateMapper;
    Lock lock=new ReentrantLock();
    /**
     *@Description:通过pk查询一条记录
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate selectByPk(String pk) {
        try {
            //新建工种实体
            AnalysisItems analysisItems = null;
            analysisItems = analysisItemsMapper.selectByPk(pk);
            //判断查表操作是否成功
            if (null != analysisItems) {
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(analysisItems).build();
            }
            else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    /**
     *@Description:通过镀槽编号查询记录
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate selectByTankNo(String page,String limit,String tankNo) {
        if (Validator.isNumber(page) && Validator.isNumber(limit) && !("0".equals(page) && "0".equals(limit))) {
            try {
                //开始分页
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                //新建化验项目类
                List<AnalysisItems> list = new ArrayList<>();
                list = analysisItemsMapper.selectByTankNo(tankNo);
                PageInfo<AnalysisItems> info=new PageInfo<>(list);
                if (info.getTotal()>0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count(info.getTotal()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }

        } else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }
    /**
     *@Description:插入一条数据
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate insert(AnalysisItems analysisItems) {
        try {
            lock.lock();
            int rows = analysisItemsMapper.insert(analysisItems);

            if (rows > 0) {
                return ResponseTemplate.builder().code(0).message("插入数据成功").count(1l).build();
            } else {//没有异常并且插入数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("插入数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
        finally {
            lock.unlock();
        }
    }
    /**
     *@Description:插入一组数据
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate batchInsert(List<AnalysisItems> listAnalysisItems) {
        try {
            lock.lock();
            int rows = analysisItemsMapper.batchInsert(listAnalysisItems);
            AnalysisItems analysisItems = listAnalysisItems.get(0);

            Tank tank = tankMapper.selectByTankNo(analysisItems.getTankNo());
            //通过记录表的最新记录新建一个操作表的记录
            Operate operate = new Operate();
            operate.setTankNo(tank.getTankNo());
            operate.setTankName(tank.getTankName());
            operate.setBathVolue(tank.getBathVolume());
            operate.setTankResponsible(tank.getTankResponsible());
            operate.setTypeWorkCode(tank.getTypeWorkCode());
            operate.setTypeWorkName(tank.getTypeWorkName());
            operate.setDeleteFlag("1");
           /* if("3".equals(analysisItems.getOthers())){
                operate.setTechnologist("1");
            }else {

            }*/
            operate.setTechnologist(analysisItems.getTechnologist());
            operate.setOriginItemsList(tank.getOriginItemsList());
            //如果其他的值为3则说明调整员不需要调整
            if ("3".equals(analysisItems.getOthers())&&"1".equals(analysisItems.getAnalyst())) {
                operate.setAnalyst(analysisItems.getAnalyst());
                operate.setOperator("1");
            }else {
                operate.setAnalyst(analysisItems.getAnalyst());
                operate.setOperator(analysisItems.getOperator());
            }
            if ("2".equals(analysisItems.getOperator())){

            }
            operate.setInspector(analysisItems.getInspector());
            operate.setOperateID(analysisItems.getOperateId());
            int i = operateMapper.rDelete(operate.getOperateID());
            int insert = operateMapper.insert(operate);
            if (rows > 0) {
                return ResponseTemplate.builder().code(0).message("插入数据成功").count(1l).build();
            } else {//没有异常并且插入数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("插入数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
        finally {
            lock.unlock();
        }
    }
    /**
     *@Description:更新一组信息
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate batchUpdate(List<AnalysisItems> analysisItemsList) {
        try {
            lock.lock();
            //没有异常并且修改数据条数大于0
            int rows = analysisItemsMapper.batchUpdate(analysisItemsList);
            if(rows>0) {
                return ResponseTemplate.builder().code(0).message("修改数据成功").count(1l).build();
            }
            else {//没有异常并且修改数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("修改数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
        finally {
            lock.unlock();
        }
    }

    /**
     *@Description:更新一条信息
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate updateByItems(AnalysisItems analysisItems) {
        try {
            lock.lock();
            //没有异常并且修改数据条数大于0
            int rows = analysisItemsMapper.updateByItems(analysisItems);
            if(rows>0) {
                return ResponseTemplate.builder().code(0).message("修改数据成功").count(1l).build();
            }
            else {//没有异常并且修改数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("修改数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
        finally {
            lock.unlock();
        }
    }

    /**
     *@Description:通过基础化验项目表更新一条信息
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate oBatchUpdate(String tankNo,String analysisItems,OriginItems originItems) {
        try {
            lock.lock();
            //没有异常并且修改数据条数大于0
            int rows = analysisItemsMapper.oBatchUpdate(tankNo, analysisItems, originItems);
            if(rows>0) {
                return ResponseTemplate.builder().code(0).message("修改数据成功").count(1l).build();
            }
            else {//没有异常并且修改数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("修改数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
        finally {
            lock.unlock();
        }
    }
}
